﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using Entities;
using DevExpress.XtraReports.UI;

namespace QLTTTH
{
    public partial class UCDiemThiBC : DevExpress.XtraEditors.XtraUserControl
    {
        public enum DKTimDiem
        {
            TheoKhoaHoc,
            TheoHocVien,
            TheoKyThi,
            TheoKetQua
        }

        private DKTimDiem dieuKien;
        public bool isStatistics = false;

        public UCDiemThiBC()
        {
            InitializeComponent();
        }

        private void LoadDsDiemThi()
        {
            var dsDT = DiemThiManager.LayDanhSachDiemThi();
            if (dsDT.Success)
            {
                LoadDsDiemThi(dsDT);
            }
        }

        private void LoadDsDiemThi(ActionResultAsList<DiemThi> dsHP)
        {
            dgvDiemThi.DataSource = dsHP.Result;
        }

        private void UCDiemThiBC_Load(object sender, EventArgs e)
        {
            LoadDsDiemThi();
            InitControl();
        }

        private void InitControl()
        {
            var dsKH = KhoaHocManager.LayDanhSachKhoaHoc();
            if (dsKH.Success)
            {
                loupKhoaHoc.Properties.DataSource = dsKH.Result;
                loupKhoaHoc.Properties.ValueMember = "MaKH";
                loupKhoaHoc.Properties.DisplayMember = "TenKH";

            }

            var dsHV = HocVienManager.LayDanhSachHocVien();
            if (dsHV.Success)
            {
                loupHocVien.Properties.DataSource = dsHV.Result;
                loupHocVien.Properties.ValueMember = "MaHV";
                loupHocVien.Properties.DisplayMember = "HoTen";
            }

            var dsKT = KyThiManager.LayDanhSachKyThi();
            if (dsKT.Success)
            {
                loupKyThi.Properties.DataSource = dsKT.Result;
                loupKyThi.Properties.ValueMember = "MaKyThi";
                loupKyThi.Properties.DisplayMember = "NgayThi";
            }
        }

        private void loupKhoaHoc_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimDiem.TheoKhoaHoc;
            isStatistics = true;
        }

        private void loupHocVien_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimDiem.TheoHocVien;
            isStatistics = true;
        }

        private void loupKyThi_EditValueChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimDiem.TheoKyThi;
            isStatistics = true;
        }
        private void cboKetQua_SelectedIndexChanged(object sender, EventArgs e)
        {
            dieuKien = DKTimDiem.TheoKetQua;
            isStatistics = true;
        }

        private void btnThongKe_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (!isStatistics)
            {
                XtraMessageBox.Show("Chưa chọn nội dung thống kê", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            ActionResultAsList<DiemThi> aResult;
            if (!string.IsNullOrEmpty(dieuKien.ToString()))
            {
                switch (dieuKien)
                {
                    case DKTimDiem.TheoKhoaHoc:
                        aResult = DiemThiManager.TimDanhSachDiem(loupKhoaHoc.EditValue,
                            (object a, object b) =>
                            {
                                return (b as DiemThi).MaKH.CompareTo(a);
                            });
                        if (aResult.Success)
                        {
                            LoadDsDiemThi(aResult);
                        }
                        break;
                    case DKTimDiem.TheoHocVien:
                        aResult = DiemThiManager.TimDanhSachDiem(loupHocVien.EditValue,
                            (object a, object b) =>
                            {
                                return (b as DiemThi).MaHV.CompareTo(a);
                            });
                        if (aResult.Success)
                        {
                            LoadDsDiemThi(aResult);
                        }
                        break;
                    case DKTimDiem.TheoKyThi:
                        aResult = DiemThiManager.TimDanhSachDiem(loupKyThi.EditValue,
                            (object a, object b) =>
                            {
                                return (b as DiemThi).MaKyThi.CompareTo(a);
                            });
                        if (aResult.Success)
                        {
                            LoadDsDiemThi(aResult);
                        }
                        break;
                    case DKTimDiem.TheoKetQua:
                        aResult = DiemThiManager.TimDanhSachDiem(cboKetQua.Text.ToString(),
                            (object a, object b) =>
                            {
                                return (b as DiemThi).KetQua.CompareTo(a);
                            });
                        if (aResult.Success)
                        {
                            LoadDsDiemThi(aResult);
                        }
                        break;
                }
            }
        }

        private void btnIn_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            var rpt = new rptDiemThiBC();
            rpt.DataSource = dgvDiemThi.DataSource;
            var tool = new ReportPrintTool(rpt);
            tool.ShowPreview();
        }

        private void btnRefresh_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            LoadDsDiemThi();
            isStatistics = false;
        }

    }
}
